package com.yang.job.utils;


import java.util.HashMap;
import java.util.Map;

public class FeaturesUtils {
    private final static String KEY_KEY_SEPARATOR = ";";
    private final static String KEY_VALUE_SEPARATOR = ":";

    public static Map<String, String> convert2FeatureMap(String features) {
        Map<String, String> featureMap = new HashMap<>();
        if (features == null || features.isEmpty()) {
            return featureMap;
        }
        String[] keyValues = features.split(KEY_KEY_SEPARATOR);
        for (String keyValue : keyValues) {
            String[] split = keyValue.split(KEY_VALUE_SEPARATOR);
            String key = split[0];
            String value = split[1];
            featureMap.put(key, value);
        }
        return featureMap;
    }

    public static String convert2Features(Map<String, String> featureMap) {
        if (featureMap == null || featureMap.isEmpty()) {
            return "";
        }
        StringBuilder stringBuilder = new StringBuilder();
        featureMap.forEach((key, value) -> {
            stringBuilder.append(key)
                    .append(KEY_VALUE_SEPARATOR)
                    .append(value)
                    .append(KEY_KEY_SEPARATOR);
        });
        return stringBuilder.toString();
    }
}
